package com.sxkiler.demo.web.controller;

import com.google.common.collect.ImmutableMap;
import com.sxkiler.demo.web.service.IUserService;
import com.sxkiler.demo.web.service.LazyService;
import com.sxkiler.demo.web.service.impl.LazyServiceImpl;
import com.sxkiler.demo.web.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/com.sxkiler.demo.test")
public class TestController {
    private Logger logger = LoggerFactory.getLogger(TestController.class);
    public TestController(){
        logger.info("======================= lazyService init");
    }
    @Autowired
    LazyService lazyService;
    @Autowired
    IUserService userService;

    @RequestMapping("/hello")
    @ResponseBody
    public Object hello(){
        return ImmutableMap.of("com.sxkiler.demo.test","测试");
    }


    @RequestMapping("/lazy")
    @ResponseBody
    public Object lazy(){
        lazyService.lazyLoad();
        return new HashMap<>();
    }
    @RequestMapping("/sleep")
    @ResponseBody
    public Object sleep(@RequestParam(name = "sleepTime") Long sleepTime) throws InterruptedException {
        logger.info("sleepTime={},{}",sleepTime,Thread.currentThread().getId());
        Thread.sleep(sleepTime);
        logger.info("sleepTimeFinish={},{}",sleepTime,Thread.currentThread().getId());
        return new HashMap<>();
    }

    @RequestMapping("/getUser")
    public UserVo getUser(String number){
        return userService.findUserByNumber(number);
    }
}
